package com.tang.thepath.service;


import com.tang.thepath.model.BaseResponse;
import com.tang.thepath.model.RequestModel;

import retrofit2.http.Body;
import retrofit2.http.POST;
import rx.Observable;
import rx.functions.Func1;

public class AuthService extends BaseService {

    private interface API {
        @POST("/api/login")
        Observable<BaseResponse<RequestModel.LoginResponse>> login(@Body RequestModel.LoginRequest request);

        @POST("/api/member/changepassword")
        Observable<BaseResponse<RequestModel.IdResponse>> changePassword(@Body RequestModel.ChangePasswordRequest request);
    }


    public static Observable<Boolean> login(RequestModel.LoginRequest request){

        return toSubscribe(HttpManager.getService(API.class).login(request))
                .map(new Func1<RequestModel.LoginResponse, Boolean>() {
                    @Override
                    public Boolean call(RequestModel.LoginResponse response) {
                        JWTHolder holder = JWTHolder.getInstance();
                        holder.setToken(response.token);
                        holder.setRole(response.role);
                        holder.setLevel(response.level);
                        holder.setExpired(response.expired);
                        holder.setMemberId(response.memberId);
                        return true;
                    }
                });
    }

    public static Observable<RequestModel.IdResponse> changePassword(RequestModel.ChangePasswordRequest request){
        return toSubscribe(HttpManager.getService(API.class).changePassword(request));
    }
}
